package com.changgou.canal.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.content.feign.ContentFeign;
import com.changgou.content.pojo.Content;
import com.changgou.util.Result;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * @Author tang.qingbo
 * @Date 2020/8/26 23:27
 * @Version 1.0
 */
@CanalEventListener//开启监听
public class CanalDataEventListener {

    @Autowired
    private ContentFeign contentFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 增加数据监听
     */
    //@InsertListenPoint
    public void onInsertEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("==================================增加操作==================================");

        //获取添加后的数据
        rowData.getAfterColumnsList().forEach(column -> System.out.println("添加后的数据: "+column.getName() + " " + column.getValue()));
    }

    /**
     * 修改数据监听
     */
    //@UpdateListenPoint
    public void onUpdateEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("==================================修改操作==================================");
        //修改前的数据
        rowData.getBeforeColumnsList().forEach(column -> System.out.println("修改前的数据: "+column.getName() + " " + column.getValue()));

        System.out.println("----------------------------------------------------------------------------");

        //修改后的数据
        rowData.getAfterColumnsList().forEach(column -> System.out.println("修改后的数据: " + column.getName() + " " + column.getValue()));
    }

    /**
     * 删除数据监听
     */
    //@DeleteListenPoint
    public void onDeleteEvent(CanalEntry.RowData rowData) {
        System.out.println("==================================删除操作==================================");
        //删除前的数据
        rowData.getBeforeColumnsList().forEach(column -> System.out.println("删除的数据: "+column.getName() + " " + column.getValue()));
    }

    /**
     * 自定义数据监听
     */
    @ListenPoint(destination = "example",
            schema = "changgou_content",//库名
            table = {"tb_content","tb_content_category"},//指定表
            eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE, CanalEntry.EventType.UPDATE})//类型
    public void onAllEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        /*rowData.getBeforeColumnsList().forEach(column -> System.out.println("操作前的数据: "+column.getName() + " " + column.getValue()));
        rowData.getAfterColumnsList().forEach(column -> System.out.println("操作后的数据: "+column.getName() + " " + column.getValue()));*/

        //获取广告的分类id
        String categoryId = getColumn(rowData, "category_id");
        Result<List<Content>> result = contentFeign.findByCategoryId(Long.parseLong(categoryId));

        //获取数据
        List<Content> data = result.getData();
        //放入redis
        stringRedisTemplate.boundValueOps("content_" + categoryId).set(JSON.toJSONString(data));
    }

    private String getColumn(CanalEntry.RowData rowData, String field) {
        //获取之后的数据
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if (field.equals(column.getName())) {
                return column.getValue();
            }
        }
        //操作前的数据
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            if (field.equals(column.getName())) {
                return column.getValue();
            }
        }
        return null;
    }
}
